<?php

/*
 * This class is used for as Model for Hardware table 
 */

class Model_Hardware extends Model_Base {

    protected $_name = 'tbl_hardware';
    protected $_primaryKey = 'hard_id';

    
    //get site data
    public function getbuildingArray() {
        $buildingModel = new Model_Building();
        $buildingArray = $buildingModel->fetchBuilding();
        $finalArray[''] = '';
        foreach ($buildingArray as $com) {
            $finalArray[$com['build_id']] = $com['build_name'];
        }
        return $finalArray;
    }
    
    
    //get siteing data
    public function getsiteingArray() {
        //siteing model
        $siteingModel = new Model_Building();
        $siteingArray = $siteingModel->fetchBuilding();
        $finalArray[''] = '';
        foreach ($siteingArray as $com) {
            $finalArray[$com['site_id']] = $com['site_name'];
        }
        return $finalArray;
    }

    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '-- Select Country --';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Hardware
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
           # '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('hardware' => 'tbl_hardware'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = hardware.updated_lgn_id")
                ->join(array('build' => 'tbl_buildings'), "build.build_id = hardware.build_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = build.site_id",array('com_id'))
                ->where('hard_id = ?', $id)
                ->where('hard_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('hardware' => 'tbl_hardware'))
                ->setIntegrityCheck(false)
                ->where('hard_id = ?', $id)
                ->where('hard_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get hardwares
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchHardware($where = "", $limit = "", $offset = "0", $order = 'hard_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('hardware' => 'tbl_hardware'))
                ->setIntegrityCheck(false)
                ->join(array('build' => 'tbl_buildings'), "build.build_id = hardware.build_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = build.site_id",array('com_id'))
                ->where('hard_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getHardwareCount($where) {
        $select = $this->select()
                ->from(array('hardware' => 'tbl_hardware'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('build' => 'tbl_buildings'), "build.build_id = hardware.build_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = build.site_id",array('com_id'))
                ->where('hard_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete hardwares
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteHardware($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_hardware) {
                $hardwareData = Array();
                $hardwareData = $this->fetchEntryById($_hardware);
                if (count($hardwareData) > 0) {
                    $data = array('hard_status' => '2');
                    $this->update($data, 'hard_id =' . (int) $_hardware);
                }
            }
        } else {
            $hard_id = $prdtArray;
            $hardwareData = $this->fetchEntryById($hard_id);
            if (count($hardwareData) > 0) {
                $data = array('hard_status' => '2');
                $this->update($data, 'hard_id =' . $hard_id);
            }
        }
    }

}
